## ---------------------------------------------------------------------- #
## Figure 4
## Christopher J. Fariss
## "Yes, Human Rights Practices Are Improving Over Time"
## American Political Science Review
## ---------------------------------------------------------------------- #
rm(list = ls())

library(rstan)

load("M_All.RData")
#test <- summary(mod)$summary
#write.csv(summary(mod)$summary,"summary_M_All.csv")
test <- read.csv("summary_M_All.csv")
tmp <- subset(test, grepl("theta", test$X), select=c(mean, sd, X2.5., X97.5.))

data3  <- data[order(data$id),]
data3$latentmean <- tmp$mean
data3$latentsd <- tmp$sd
data3$upper <- tmp$X2.5.
data3$lower <- tmp$X97.5.
data3 <- subset(data3, YEAR>=1946)


load("M_Fixed.RData")
#test <- summary(mod)$summary
#write.csv(summary(mod)$summary,"summary_M_Fixed.csv")
test <- read.csv("summary_M_Fixed.csv")
tmp <- subset(test, grepl("theta", test$X), select=c(mean, sd, X2.5., X97.5.))

data4  <- data[order(data$id),]
data4$latentmean <- tmp$mean
data4$latentsd <- tmp$sd
data4$upper <- tmp$X2.5.
data4$lower <- tmp$X97.5.
data4 <- subset(data4, YEAR>=1946)


load("M_Standards.RData")
#test <- summary(mod)$summary
#write.csv(summary(mod)$summary,"summary_M_Standards.csv")
test <- read.csv("summary_M_Standards.csv")
tmp <- subset(test, grepl("theta", test$X), select=c(mean, sd, X2.5., X97.5.))

data5  <- data[order(data$id),]
data5$latentmean <- tmp$mean
data5$latentsd <- tmp$sd
data5$upper <- tmp$X2.5.
data5$lower <- tmp$X97.5.
data5 <- subset(data5, YEAR>=1946)


## only events-based variables
load("progressive_summary_events.RData")
test <- read.csv("progressive_summary_events.csv")
tmp <- subset(test, grepl("theta", test$X), select=c(mean, sd, X2.5., X97.5.))


data1  <- data[order(data$id),]
data1$latentmean <- tmp$mean
data1$latentsd <- tmp$sd
data1$upper <- tmp$X2.5.
data1$lower <- tmp$X97.5.
data1 <- subset(data1, YEAR>=1946)


## only reports-based variables
load("progressive_no_events_summary_state.RData")
test <- read.csv("progressive_no_events_summary_state.csv")
tmp <- subset(test, grepl("theta", test$X), select=c(mean, sd, X2.5., X97.5.))


data2  <- data[order(data$id),]
data2$latentmean <- tmp$mean
data2$latentsd <- tmp$sd
data2$upper <- tmp$X2.5.
data2$lower <- tmp$X97.5.
data2 <- subset(data2, YEAR>=1946)


cor(data1$latentmean,data2$latentmean)
cor(cbind(data1$latentmean, data2$latentmean, data3$latentmean, data4$latentmean, data5$latentmean))




colfunc <- colorRampPalette(c("#b35806","#f1a340","#fee0b6","#d8daeb","#998ec3","#542788"))
COLOR <- colfunc(40)

colfunc <- colorRampPalette(c(grey(.95), grey(.55)))
COLOR <- colfunc(40)


COLOR.unit <- NA
TIME <- data1$YEAR - 1975

for(i in 1:length(COLOR)){
    COLOR.unit[TIME==i] <- COLOR[i]
}

#COLOR.unit <- grey(.7)

par(mfrow=c(5,5), mar=c(1.75,1.75,.5,.5))


plot(0,0,xlab="", ylab="", type="n", yaxt="n", xaxt="n", bty="n")
text(0,.5, expression(italic("Constant\nStandard")), cex=1.5)
text(0,-.25, "Standards-based\nvariables only", cex=1.25)
plot(data1$latentmean, data2$latentmean, pch=1, col=COLOR.unit, bg=grey(.95), cex=.75, xlab="", ylab="", ylim=c(-4.75,4.75), xlim=c(-4.75,4.75), lwd=.1)
text(-3,4, round(cor(data1$latentmean, data2$latentmean), digits=3), cex=1.5)
abline(a=0,b=1,col=1)
plot(0,0,xlab="", ylab="", type="n", yaxt="n", xaxt="n", bty="n")
plot(0,0,xlab="", ylab="", type="n", yaxt="n", xaxt="n", bty="n")
plot(0,0,xlab="", ylab="", type="n", yaxt="n", xaxt="n", bty="n")

plot(0,0,xlab="", ylab="", type="n", yaxt="n", xaxt="n", bty="n")
text(0,.5, expression(italic("All Varying\nStandard")), cex=1.5)
text(0,-.25, "All variables", cex=1.25)
plot(data1$latentmean, data3$latentmean, pch=1, col=COLOR.unit, bg=grey(.95), cex=.75, xlab="", ylab="", ylim=c(-4.75,4.75), xlim=c(-4.75,4.75), lwd=.1)
text(-3,4, round(cor(data1$latentmean, data3$latentmean), digits=3), cex=1.5)
abline(a=0,b=1,col=1)
plot(data2$latentmean, data3$latentmean, pch=1, col=COLOR.unit, bg=grey(.95), cex=.75, xlab="", ylab="", ylim=c(-4.75,4.75), xlim=c(-4.75,4.75), lwd=.1)
text(-3,4, round(cor(data2$latentmean, data3$latentmean), digits=3), cex=1.5)
abline(a=0,b=1,col=1)
plot(0,0,xlab="", ylab="", type="n", yaxt="n", xaxt="n", bty="n")
plot(0,0,xlab="", ylab="", type="n", yaxt="n", xaxt="n", bty="n")


plot(0,0,xlab="", ylab="", type="n", yaxt="n", xaxt="n", bty="n")
text(0,.5, expression(italic("Constant\nStandard")), cex=1.5)
text(0,-.25, "All variables", cex=1.25)
plot(data1$latentmean, data4$latentmean, pch=1, col=COLOR.unit, bg=grey(.95), cex=.75, xlab="", ylab="", ylim=c(-4.75,4.75), xlim=c(-4.75,4.75), lwd=.1)
text(-3,4, round(cor(data1$latentmean, data4$latentmean), digits=3), cex=1.5)
abline(a=0,b=1,col=1)
plot(data2$latentmean, data4$latentmean, pch=1, col=COLOR.unit, bg=grey(.95), cex=.75, xlab="", ylab="", ylim=c(-4.75,4.75), xlim=c(-4.75,4.75), lwd=.1)
text(-3,4, round(cor(data2$latentmean, data4$latentmean), digits=3), cex=1.5)
abline(a=0,b=1,col=1)
plot(data3$latentmean, data4$latentmean, pch=1, col=COLOR.unit, bg=grey(.95), cex=.75, xlab="", ylab="", ylim=c(-4.75,4.75), xlim=c(-4.75,4.75), lwd=.1)
text(-3,4, round(cor(data3$latentmean, data4$latentmean), digits=3), cex=1.5)
abline(a=0,b=1,col=1)
plot(0,0,xlab="", ylab="", type="n", yaxt="n", xaxt="n", bty="n")


plot(0,0,xlab="", ylab="", type="n", yaxt="n", xaxt="n", bty="n")
text(0,.5, expression(italic("Changing\nStandard")), cex=1.5)
text(0,-.25, "All variables", cex=1.25)
plot(data1$latentmean, data5$latentmean, pch=1, col=COLOR.unit, bg=grey(.95), cex=.75, xlab="", ylab="", ylim=c(-4.75,4.75), xlim=c(-4.75,4.75), lwd=.1)
text(-3,4, round(cor(data1$latentmean, data5$latentmean), digits=3), cex=1.5)
abline(a=0,b=1,col=1)
plot(data2$latentmean, data5$latentmean, pch=1, col=COLOR.unit, bg=grey(.95), cex=.75, xlab="", ylab="", ylim=c(-4.75,4.75), xlim=c(-4.75,4.75), lwd=.1)
text(-3,4, round(cor(data2$latentmean, data5$latentmean), digits=3), cex=1.5)
abline(a=0,b=1,col=1)
plot(data3$latentmean, data5$latentmean, pch=1, col=COLOR.unit, bg=grey(.95), cex=.75, xlab="", ylab="", ylim=c(-4.75,4.75), xlim=c(-4.75,4.75), lwd=.1)
text(-3,4, round(cor(data3$latentmean, data5$latentmean), digits=3), cex=1.5)
abline(a=0,b=1,col=1)
plot(data4$latentmean, data5$latentmean, pch=1, col=COLOR.unit, bg=grey(.95), cex=.75, xlab="", ylab="", ylim=c(-4.75,4.75), xlim=c(-4.75,4.75), lwd=.1)
text(-3,4, round(cor(data4$latentmean, data5$latentmean), digits=3), cex=1.5)
abline(a=0,b=1,col=1)


plot(0,0,xlab="", ylab="", type="n", yaxt="n", xaxt="n", bty="n")
plot(0,0,xlab="", ylab="", type="n", yaxt="n", xaxt="n", bty="n")
text(0,.25, expression(italic("Constant\nStandard")), cex=1.5)
text(0,-.5, "Events-based\nvariables only", cex=1.25)
plot(0,0,xlab="", ylab="", type="n", yaxt="n", xaxt="n", bty="n")
text(0,.25, expression(italic("Constant\nStandard")), cex=1.5)
text(0,-.5, "Standards-based\nvariables only", cex=1.25)
plot(0,0,xlab="", ylab="", type="n", yaxt="n", xaxt="n", bty="n")
text(0,.25, expression(italic("All Varying\nStandard")), cex=1.5)
text(0,-.5, "All variables", cex=1.25)
plot(0,0,xlab="", ylab="", type="n", yaxt="n", xaxt="n", bty="n")
text(0,.25, expression(italic("Constant\nStandard")), cex=1.5)
text(0,-.5, "All variables", cex=1.25)
